﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class CategoryService : BaseService<Category>, ICategoryService
    {
        public CategoryService()
            : base(RepositoryFactory.CategoryRepository)
        {
        }

        public IQueryable<Category> FindList(int number, string name, int restaurant_Id, int orderCode)
        {
            return FindPageList(out number, 1, number, name, restaurant_Id, orderCode);
        }

        public IQueryable<Category> FindPageList(out int totalRecord, int pageIndex, int pageSize, string name, int restaurant_Id, int orderCode)
        {
            IQueryable<Category> category = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(name)) category = category.Where(cm => cm.Name.Contains(name));
            if (restaurant_Id > 0) category = category.Where(s => s.Restaurant_Id == restaurant_Id);

            category = Order(category, orderCode);
            totalRecord = category.Count();

            return PageList(category, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<Category> Order(IQueryable<Category> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.Name);
                    break;
            }
            return entitys;
        }
    }
}
